package com.lc.luzhengtang;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.bumptech.glide.request.target.ViewTarget;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.lc.luzhengtang.db.BaseDB;
import com.lc.luzhengtang.util.WXPayUtils;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.common.QueuedWork;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.app.AppInit;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.Http;

/**
 * name 项目名称
 * log 是否打印
 * initialize 是否网络初始化
 * width 适配宽度
 * scale 适配字体倍数
 */
@AppInit(name = "super_helper", log = false, initialize = false, width = 720, scale = 1f)
public class BaseApplication extends AppApplication {
    private static final String TAG = "BaseApplication";
    private static Context mContext;
    public static BasePreferences basePreferences;
    public static BasePreferences2 basePreferences2;
    public static BaseDB baseDB;
    //
//    public static UtilAsyncSDCardBitmap UtilAsyncSDCardBitmap;
    public static BasePreferences guidePreferences;
    // 微信AppId
    // 微信AppId
    public static WXPayUtils mWxPay;

    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        Fresco.initialize(this);
//        try {
//            //https双向证书
//            Http.getInstance().setCertificates(null, getAssets().open("client2222.p12"), "123123", "PKCS12");
//        } catch (IOException e1) {
//            e1.printStackTrace();
//        }

        baseDB = new BaseDB(this);
        //网络请求等待框，可自定义
        mContext = this;
        ;
        UMShareAPI.get(this);
        //开启debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看，正式发布，请关闭该模式
        Config.DEBUG = false;
        QueuedWork.isUseThreadPool = false;
        PlatformConfig.setWeixin("wx634680d850cc583a", "c715f067ba17a2a1cc2e2470012e0c53");
        PlatformConfig.setQQZone("1106143345", "Jnb4SBmvWhiAOgNb");

        CrashReport.initCrashReport(getApplicationContext(), "b5026c0ff7", false);
        Http.getInstance().setOnStartEndCreateDialog(new Http.OnStartEndCreateDialog() {

            @Override
            public Dialog onCreate(Context context) {

                return new ProgressDialog(context);

            }

            @Override
            public void show(Dialog dialog) {
                dialog.show();
            }

            @Override
            public void dismiss(Dialog dialog) {
                dialog.dismiss();
            }

        });

        ViewTarget.setTagId(R.id.glide_tag);
        //初始化GLide图片加载框架
        GlideLoader.getInstance().init(this, R.mipmap.zhanwei);

        //初始化自定首选项
        basePreferences = new BasePreferences(this);
        basePreferences2 = new BasePreferences2(this);
        //初始化自定义数据库
        guidePreferences = new BasePreferences(this);
        initWei();
        initCloudChannel(this);
    }


    /**
     * 获取全局的context
     */
    public static Context getContext() {
        return mContext;
    }

    private void initWei() {
        mWxPay = new WXPayUtils(this);
    }


    /**
     * 初始化云推送通道
     *
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }

}
